package xyf.p07.binsearch;

/**
 * ClassName: P0852_PeakIndexInMountainArray
 * Description:
 * Author: xyf
 * Date: 9/7/22 4:54 PM
 * Version: 1.0
 **/
public class P0852_PeakIndexInMountainArray
{
    public int peakIndexInMountainArray(int[] arr)
    {
        int start = 0;
        int end = arr.length - 1;
        while (start <= end)
        {
            int middle = (end - start) / 2 + start;

            if (middle == 0)
            {
                start = middle + 1;
            }
            else if (middle == arr.length - 1)
            {
                end = middle - 1;
            }
            else if (arr[middle] > arr[middle - 1] && arr[middle] > arr[middle + 1])
            {
                return middle;
            }
            else if (arr[middle] < arr[middle - 1])
            {
                end = middle - 1;
            }
            else
            {
                start = middle + 1;
            }
        }
        return -1;
    }
}